Skip to content

Commit

Permalink
Generate .js files in addition to .mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Jun 13, 2019
1 parent 7fcd2e5 commit 01bfb85
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 108 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local-builds/
firebase-debug.log
.firebase

# Generated `.mjs` files from TypeScript source
# Generated files from TypeScript source
packages/workbox-core/**/*.js
packages/workbox-core/**/*.mjs
packages/workbox-core/**/*.d.ts
2 changes: 1 addition & 1 deletion gulp-tasks/build-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const cleanPackage = async (packagePath) => {
if (await fs.pathExists(path.join(packagePath, 'src', 'index.ts'))) {
// Store the list of deleted files, so we can delete directories after.
const deletedPaths = await del([
path.posix.join(packagePath, '**/*.+(mjs|d.ts)'),
path.posix.join(packagePath, '**/*.+(js|mjs|d.ts)'),
// Don't delete files in node_modules
'!**/node_modules', '!**/node_modules/**/*',
]);
Expand Down
39 changes: 26 additions & 13 deletions gulp-tasks/transpile-typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,30 @@ const {AsyncDebounce} = require('../infra/utils/AsyncDebounce');
const packagesDir = path.resolve('packages');

/**
* Returns a Rollup plugin that replaces extensions within filenames and
* imports. This is a bit of a hack, but it's needed until to solve:
* Returns a Rollup plugin that generates both `.js` and `.mjs` files for each
* `.ts` source file. This is a bit of a hack, but it's needed until to solve:
* https://github.com/rollup/rollup/issues/2847
*
* @param {string} oldExt
* @param {string} newExt
*/
const renameImportExtensionsPlugin = (oldExt, newExt) => {
const generateMjsOutputFiles = () => {
return {
generateBundle: (options, bundle) => {
const importPattern =
new RegExp(`((?:import|export)[^']+')([^']+?)\\${oldExt}';`, 'g');
new RegExp(`((?:import|export)[^']+')([^']+?)\\.ts';`, 'g');

for (const chunkInfo of Object.values(bundle)) {
if (chunkInfo.fileName.endsWith('.ts')) {
chunkInfo.fileName = chunkInfo.fileName
.replace(new RegExp(`\\${oldExt}$`), newExt);
// Rename the `.ts` imports and filenames to `.js`;
const assetBasename = chunkInfo.fileName.replace(/.ts$/, '');

chunkInfo.fileName = assetBasename + '.js';
chunkInfo.code = chunkInfo.code.replace(importPattern, `$1$2.js';`);

chunkInfo.code = chunkInfo.code
.replace(importPattern, `$1$2${newExt}';`);
// Create mirror `.mjs` files for each `.js` file.
const mjsSource =
`export * from './${path.basename(assetBasename)}.js';`;

fs.outputFileSync(
path.join(options.dir, assetBasename + '.mjs'), mjsSource);
}
}
},
Expand All @@ -63,13 +67,19 @@ let moduleBundleCache;
* @param {string} packageName
*/
const transpilePackage = async (packageName) => {
const packagePath = path.join(packagesDir, packageName);
console.log({packageName});

const packagePath = path.join(packagesDir, packageName);
const input = path.join(packagePath, 'src', 'index.ts');

console.log({packagesDir});
console.log({packagePath});
console.log({input});

const plugins = [
resolve(),
typescript2(),
renameImportExtensionsPlugin('.ts', '.mjs'),
generateMjsOutputFiles(),
];

const bundle = await rollup({
Expand Down Expand Up @@ -97,6 +107,9 @@ const transpilePackage = async (packageName) => {
* @param {string} packagePath
*/
const transpilePackagesOrSkip = async (packagePath) => {
console.log('path.sep', path.sep);
console.log(packagePath.split(path.sep));

const packageName = packagePath.split(path.sep).slice(-1)[0];

if (await fs.pathExists(path.join(packagePath, 'src'))) {
Expand Down
9 changes: 6 additions & 3 deletions infra/testing/server/routes/build-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ async function handler(req, res) {
} else {
const pkg = outputFilenameToPkgMap[packageFile];

// If the pkg.module references something in the build directory, use
// that, otherwise use pkg.main.
// If the pkg.module or pkg.main references something in the build
// directory, use that. Otherwise base the build file on the pkg name.

if (pkg.module && pkg.module.startsWith('build/')) {
file = path.join(pkgDir, pkg.module);
} else {
} else if (pkg.main && pkg.main.startsWith('build/')) {
file = path.join(pkgDir, pkg.main);
} else {
file = path.join(pkgDir, 'build', `${pkg.name}.prod.js`);
}

// When not specifying a dev or prod build via the filename,
Expand Down
194 changes: 129 additions & 65 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 01bfb85

Please sign in to comment.