Skip to content

Commit

Permalink
chore(rollup): Replace colons in filenames
Browse files Browse the repository at this point in the history
Windows ntfs does not like colons in filenames.
  • Loading branch information
floryst committed Mar 23, 2021
1 parent 3b10c3f commit 3da534d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"karma-tap-pretty-reporter": "4.2.0",
"karma-webpack": "git://github.com/ryanclark/karma-webpack#eec6dfc",
"kw-doc": "3.0.1",
"magic-string": "0.25.7",
"moment": "2.29.1",
"node-pre-gyp": "0.17.0",
"postcss-loader": "4.1.0",
Expand Down
59 changes: 59 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from 'path';
import * as glob from 'glob';

import autoprefixer from 'autoprefixer';
import MagicString from 'magic-string';

import alias from '@rollup/plugin-alias';
import { babel } from '@rollup/plugin-babel';
Expand All @@ -22,6 +23,56 @@ const IGNORE_LIST = [
/^Sources(\/|\\)(Testing|ThirdParty)/,
];

/**
* find: RegExp | String
* replace: String
*/
function rewriteFilenames(pluginOptions) {
const opts = {
...pluginOptions,
find: new RegExp(pluginOptions.find),
};
return {
name: 'rewrite-filenames',
generateBundle(outputOptions, bundle, isWrite) {
const files = Object.keys(bundle);
for (let i = 0; i < files.length; i++) {
const file = files[i];
const info = bundle[file];

if (opts.find.test(file)) {
const newFileName = file.replace(opts.find, opts.replace);
info.fileName = newFileName;
bundle[newFileName] = info;
delete bundle[file];
}

// search contents for offending import filenames
const importRe = new RegExp(
'(import .+? from\\s*["\'])(.+?)(["\']\\s*;?)',
'g'
);

let match;
do {
match = importRe.exec(info.code);
if (match) {
const target = match[2];
const start = match.index + match[1].length;
const end = start + target.length;
if (opts.find.test(target)) {
const magicCode = new MagicString(info.code);
const newTarget = target.replace(opts.find, opts.replace);
magicCode.overwrite(start, end, newTarget);
info.code = magicCode.toString();
}
}
} while (match);
}
},
};
}

function ignoreFile(name, ignoreList = IGNORE_LIST) {
return ignoreList.some((toMatch) => {
if (toMatch instanceof RegExp) {
Expand Down Expand Up @@ -141,5 +192,13 @@ export default {
modules: true,
plugins: [autoprefixer],
}),
// windows ntfs hates colons in filenames,
// and node-resolve and web-worker-loader are notorious for
// inserting them into virtual modules that are written out
// to the filesystem via preserveModules: true.
rewriteFilenames({
find: /:/g,
replace: '_',
}),
],
};

0 comments on commit 3da534d

Please sign in to comment.