Skip to content

Commit

Permalink
Require without file extension #397
Browse files Browse the repository at this point in the history
  • Loading branch information
tripodsan committed Feb 14, 2019
1 parent 8235e61 commit 3447f45
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
13 changes: 9 additions & 4 deletions package-lock.json

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

16 changes: 13 additions & 3 deletions src/parcel/AdapterJSAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,19 @@ class AdapterJSAsset extends JSAsset {
// return;
const isRelativeImport = /^[/~.]/.test(name);
if (isRelativeImport) {
// we mark the asset as dynamic so it won't get merged into this source.
const resolved = path.resolve(path.dirname(this.name), name);
super.addDependency(name, Object.assign({ dynamic: true, resolved }, opts));
try {
const resolved = require.resolve(name, {
paths: [
path.dirname(this.name),
],
});
// we mark the asset as dynamic so it won't get merged into this source.
super.addDependency(name, Object.assign({ dynamic: true, resolved }, opts));
} catch (e) {
// the exact stack trace is not relevant to the end user.
e.stack = null;
throw e;
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/specs/parcel/require_noext_html.htl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
this is a simple test html
-->
<!DOCTYPE html>
<!-- another comment -->
<html>
<head>
<title>Test</title>
<meta charset="utf-8">
<link rel="stylesheet" src="bla.css">
<link rel="stylesheet" src="bla.css"/>
</head>
<body>
Hello, world! this is a ${content.foo}.
</body>
</html>
30 changes: 30 additions & 0 deletions test/specs/parcel/require_noext_html.pre.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-disable */

// you can also require external modules in pre.js
// relative imports will be inlined in the generated code
const { foo } = require('./helpers');

/**
* The 'pre' function that is executed before the HTML is rendered.
* @param payload The current payload of processing pipeline
* @param payload.content The content content
*/
function pre(payload) {

payload.content.foo = foo();

}

module.exports.pre = pre;
1 change: 1 addition & 0 deletions test/testGeneratedAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const TEST_SCRIPTS = [
{ name: 'promise_html', matches: [/Hello, world/, /this is a bar/] },
{ name: 'return_promise_html', matches: [/Hello, world/, /this is a bar/] },
{ name: 'require_html', matches: [/Hello, world/, /this is a bar/] },
{ name: 'require_noext_html', matches: [/Hello, world/, /this is a bar/] },
{ name: 'simple_html', matches: [/Hello, world/, /this is a bar/] },
{ name: 'return_simple_html', matches: [/Hello, world/, /this is a bar/] },
{ name: 'xml', type: 'js', matches: [/works: bar/] },
Expand Down
3 changes: 1 addition & 2 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ async function processSource(scriptName, type = 'htl') {
path.resolve(__dirname, `specs/parcel/${scriptName}.pre.js`),
path.resolve(__dirname, 'specs/parcel/helpers.js'),
];
//
// if (await fse.pathExists(pre))

await new BuildCommand()
.withFiles(files)
.withTargetDir(buildDir)
Expand Down

0 comments on commit 3447f45

Please sign in to comment.