Giter VIP home page Giter VIP logo

import's Introduction

import("./Module")

This module allows you to write paths for Roblox instances like you do on the filesystem.

Having to constantly type script.Parent with varying levels of .Parent is tedious and can produce excessively long lines. This module aims to fix this by providing a concise syntax for writing import paths that closely resembles what's used on the filesystem.

Installation

Model File (Roblox Studio)

  • Download the rbxm model file attached to the latest release from the GitHub releases page.
  • Insert the model into Studio into a place like ReplicatedStorage

Filesystem

  • Copy the src directory into your codebase
  • Rename the folder to import
  • Use a plugin like Rojo to sync the files into a place

Usage

local import = require(game.ReplicatedStorage.Import)

-- local module = require(script.Module)
local module = import "script/Module"

-- local module = require(script.Parent.Module)
local module = import "./Module"

-- local module = require(script.Parent.Folder.Module)
local module = import "./Folder/Module"

-- local module = require(script.Parent.Parent.Module)
local module = import "../Module"

-- local module = require(script.Parent.Parent.Parent.Module)
local module = import "../../Module"

If you only care about a few members of a module, you can import them individually:

-- local foo = require(script.Parent.Module).foo
local foo = import("./Module", { "foo" })

-- local module = require(script.Parent.Module)
-- local foo = module.foo
-- local bar = module.bar
local foo, bar = import("./Module", { "foo", "bar" })

If your datamodel is set to game, children of Roblox services can be imported by starting the path with a name of a service:

-- local module = require(game:GetService("ReplicatedStorage").module)
local module = import "ReplicatedStorage/module"

-- local module = require(game:GetService("ServerStorage").module)
local module = import "ServerStorage/module"

Works for any Roblox instance, so you can use this to import assets as well:

-- local sound = script.Parent:FindFirstChild("Sound")
local sound = import "./Sound"

-- local sound = script.Parent:FindFirstChild("Part")
local part = import "./Part"

Config

You can set aliases to define starting points for your paths:

import.setConfig({
	aliases = {
		shared = game.ReplicatedStorage.Shared
	}
})

-- local module = require(game.ReplicatedStorage.Shared.Module)
local module = import "shared/Module"

You can also configure the module to use WaitForChild, with a configurable timeout

import.setConfig({
	useWaitForChild = true
	waitForChildTimeout = 1
})

You can configure the alias which you use to represent script in your paths.

import.setConfig({
	currentScriptAlias = "@"
})

-- local module = require(script.Module)
local module = import "@/Module"

By default, import will throw an error when modulescripts attempt to import eachother in a recursive loop (which would otherwise silently fail). This feature was designed with the assumption the user only has a singular script or localscript as the entry point to the codebase, and you can disable it if the feature causes problems.

import.setConfig({
	detectRequireLoops = false
})

Development

Install Rojo and then run the following commands:

$ rojo build -o place.rbxlx
$ rojo serve

Open the newly generated place file and start the Rojo plugin.

From here you can modify anything under src/ and your changes will be synced in.

When you're ready to test, simply press F5 to play.

import's People

Contributors

crossstarcross avatar reselim avatar vocksel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.