Skip to content

Commit

Permalink
Include extensions in preserveModules output filenames for scriptifie…
Browse files Browse the repository at this point in the history
…d assets
  • Loading branch information
Andarist committed Sep 15, 2019
1 parent 13b27ca commit 24fde1a
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { error } from './utils/error';
import { sortByExecutionOrder } from './utils/executionOrder';
import getIndentString from './utils/getIndentString';
import { makeLegal } from './utils/identifierHelpers';
import { basename, dirname, isAbsolute, normalize, resolve } from './utils/path';
import { basename, dirname, extname, isAbsolute, normalize, resolve } from './utils/path';
import relativeId, { getAliasName } from './utils/relativeId';
import renderChunk from './utils/renderChunk';
import { RenderOptions } from './utils/renderHelpers';
Expand Down Expand Up @@ -83,6 +83,8 @@ interface FacadeName {
name?: string;
}

const COMMON_JS_EXTENSIONS = ['.js', '.jsx', '.ts', '.tsx'];

function getGlobalName(
module: ExternalModule,
globals: GlobalsOption,
Expand Down Expand Up @@ -280,14 +282,20 @@ export default class Chunk {
options: OutputOptions,
existingNames: Record<string, any>
): string {
const sanitizedId = sanitizeFileName(this.orderedModules[0].id);
const id = this.orderedModules[0].id;
const sanitizedId = sanitizeFileName(id);

let path: string;
if (isAbsolute(this.orderedModules[0].id)) {
if (isAbsolute(id)) {
const extension = extname(id);
console.log({ COMMON_JS_EXTENSIONS, extension });
const name = renderNamePattern(
options.entryFileNames || '[name].js',
options.entryFileNames ||
(COMMON_JS_EXTENSIONS.includes(extension) ? '[name].js' : '[name].[ext].js'),
'output.entryFileNames',
{
ext: () => extension.substr(1),
extname: () => extension,
format: () => (options.format === 'es' ? 'esm' : (options.format as string)),
name: () => this.getChunkName()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
description: 'scriptified assets have extension in preserveModules output filename',
options: {
input: 'src/main.js',
preserveModules: true,
plugins: [
{
name: 'str-num-plugin',
transform(code, id) {
if (id.endsWith('.num')) {
return { code: `export default ${code.trim()}` };
}
if (id.endsWith('.str')) {
return { code: `export default "${code.trim()}"` };
}
return null;
}
}
]
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(['exports'], function (exports) { 'use strict';

var answer = 42;

exports.default = answer;

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(['exports'], function (exports) { 'use strict';

var lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

exports.default = lorem;

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
define(['exports', './answer.num', './lorem.str'], function (exports, answer, lorem) { 'use strict';



exports.answer = answer.default;
exports.lorem = lorem.default;

Object.defineProperty(exports, '__esModule', { value: true });

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

var answer = 42;

exports.default = answer;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

var lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

exports.default = lorem;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var answer = require('./answer.num.js');
var lorem = require('./lorem.str.js');



exports.answer = answer.default;
exports.lorem = lorem.default;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var answer = 42;

export default answer;

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as answer } from './answer.num.js';
export { default as lorem } from './lorem.str.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
System.register([], function (exports) {
'use strict';
return {
execute: function () {

var answer = exports('default', 42);

}
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
System.register([], function (exports) {
'use strict';
return {
execute: function () {

var lorem = exports('default', "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");

}
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
System.register(['./answer.num.js', './lorem.str.js'], function (exports) {
'use strict';
return {
setters: [function (module) {
exports('answer', module.default);
}, function (module) {
exports('lorem', module.default);
}],
execute: function () {



}
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as answer } from './answer.num';
export { default as lorem } from './lorem.str';

0 comments on commit 24fde1a

Please sign in to comment.